package com.java.shop.member.inteceptor;

import com.java.common.constant.LoginUserConstant;
import com.java.common.to.MemberTo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MemberInteceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberTo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/**", uri);
        if(match) return true;

        MemberTo memberTo = (MemberTo)request.getSession().getAttribute(LoginUserConstant.LOGIN_USER);
        if(memberTo != null){
            threadLocal.set(memberTo);
            return true;
        }else{
            response.sendRedirect("http://auth.shop.com/login.html");
            return false;
        }
    }
}
